import viz
import time
import win32gui
from win32com.shell import shell, shellcon

class Gui:
	
	def __init__(self, vizact, game):
		self.vizact = vizact
		self.game = game
		
		self.newGameButton = viz.addButton()
		self.newGameButton.setPosition(.05,.95) #Set its position.
		self.newGameButton.setScale(1,1) #Scale it.
		
		self.newGameText = viz.addText("New Game", viz.SCREEN)
		self.newGameText.setScale(0.35, 0.35)
		self.newGameText.setPosition(0.07,0.94)
		
		self.replayButton = viz.addButton()
		self.replayButton.setPosition(.05,0.88) #Set its position.
		self.replayButton.setScale(1,1) #Scale it.
		
		self.replayText = viz.addText("Open Replay", viz.SCREEN)
		self.replayText.setScale(0.35, 0.35)
		self.replayText.setPosition(0.07,0.87)
		
		self.vizact.onbuttonup(self.newGameButton, self.onClickNewGame)
		self.vizact.onbuttonup(self.replayButton, self.onClickReplay)
		
	def onClickNewGame(self):
		self.game.permissions = 'w'
		self.game.outputFile = open("results/" + (str)(time.time()).replace(".", "") + ".txt", self.game.permissions)
		self.game.myActionStack.clearActions()
		self.game.simonActionStack.clearActions()
		self.game.simonActionStack.addRandomAction()
		self.game.showSequence()
		
	def onClickReplay(self):
		mydocs_pidl = shell.SHGetFolderLocation (0, shellcon.CSIDL_PERSONAL, 0, 0)
		pidl, display_name, image_list = shell.SHBrowseForFolder (
			win32gui.GetDesktopWindow (),
			mydocs_pidl,
			"Choose a File",
			shellcon.BIF_BROWSEINCLUDEFILES,
			None,
			None
			)
		
		if pidl != None:
			path = shell.SHGetPathFromIDList(pidl)
			self.game.permissions = 'r'
			self.game.outputFile = open(path, self.game.permissions)